/***************************************************************
'*	Criado em: 23/05/2011			Por: THIAGO DE ASSIS LUCA
****************************************************************/

var Produto=
{
	init:function()
	{
		Produto.buyButton();
		//Produto.exibeFloaterCookie();
	},
	buyButton:function()
	{
		$(".buy-in-page-button, .buy-button").bind("click",function(){
			var _onclick=$(this).attr("onclick")||"";
			if(_onclick.toString().search(/alert.*\(.*selecione/i)<0)
				Produto.saveUrlCookie();
		});
	},
	saveUrlCookie:function()
	{
		var uri=(document.location.pathname)?document.location.pathname:"/";
		$.cookie("lastProductAddedToCart",uri,{"path":"/","expires":180});
	},
	exibeFloaterCookie:function()
	{
	    if($.cookie('viewFloater') == null){
            $('#bannerfloater').show();
            $.cookie('viewFloater',false,{"path":"/","expires":180})	        
        }
        else{
            $('#bannerfloater').hide();
        }
        		    
	    $('#bannerfloater').bind('click',function(){
	        $.cookie('viewFloater',false,{"path":"/","expires":180})
            $('#bannerfloater').hide();	        
	    });	    
	}	
};

$(function() {
	Produto.init();

    $('#produto .apresentacao,.skuSelection .Cor, .skuSelection .Tam ').addClass("clearfix");

    if ($('.productVideo #caracteristicas, .productVideo #caracteristicas1').children().length > 0) {
        $('#produto .apresentacao #include').append('<div id="video">' + $(".productVideo .value-field").html() + '</div><div class="thumbvideo"></div>');
        $('.thumbs').animate({ paddingLeft: '100px' }, 300);
        $('#include .thumbvideo').live('click', function() {
            var offset = $('#produto .apresentacao').offset();
            offset = (offset.top - 50) + 'px';
            $('html,body').stop(true, true).animate({ scrollTop: offset }, 1000);
            if ($.browser.msie && $.browser.version <= 8) {
                $('#include #image').css({ zIndex: "1", position: "absolute", left: "-999em" });
            } else {
                $('#include #image').animate({ opacity: 0.0, display: 'none', left: '-999em', position: 'absolute' }, 500).css("z-index", "1");
            }
            $('#include').animate({ height: '220px' }, 500);
            $('#produto .productVideo .value-field iframe').animate({ top: '-208px' }, 500);
            if ($.browser.msie && $.browser.version <= 8) {
                $('#include #video').css({ left: 0, opacity: 1, display: 'block', position: 'relative', zIndex: 2 });
            }
            else {
                $('#include #video').animate({ zIndex: "2", left: '0px', opacity: 1.0, display: 'block', position: 'relative' }, 500);
            }

        });

        $('.thumbs li').live('click', function() {
            $('#include').animate({ height: '378px' }, 500);
            $('#produto .productVideo .value-field iframe').animate({ top: '-108px' }, 500);
            if ($.browser.msie && $.browser.version <= 8) {
                $('#include #video').css({ left: '-999em', opacity: 0, display: 'none', position: 'absolute', zIndex: 1 });
            } else {
                $('#include #video').animate({ opacity: 0.0, display: 'none', left: '-999em', position: 'absolute' }, 10).css("z-index", "1");
            }
            if ($.browser.msie && $.browser.version <= 8) {
                $('#include #image').css({ zIndex: "2", position: "relative", left: "0" });
            } else {
                $('#include #image').animate({ opacity: 1.0, display: 'block', left: '0', position: 'relative' }, 500).css("z-index", "2");
            }
        });
    }

    $('#produto .apresentacao #include').append('')

    $('.lista-abas li').each(function() {
        //alert($(this).text().substr(0, 30));
        $(this).attr('title', $(this).text());
        $(this).text(($(this).text().length > 30) ? $(this).text().substr(0, 30) + "..." : $(this).text().substr(0, 30));
    });

    $('.lista-abas li').live('click', function() {
        if ($(this).attr('class').indexOf('active') == -1) {
            $('.lista-abas li').removeClass("active");
            $(this).addClass("active");
            $('.abas > div').hide();
            $('.abas > div').eq($(this).index()).fadeIn();
        }
        else {
            return false;
        }
    });

    if ($('#divCompreJunto table tr').length > 1) {
        $('#divCompreJunto table tr:not(:last) td').addClass('bordaTD');
        $('#divCompreJunto hr').hide();
    }

    $('.skuList label').each(function() {
        $(this).attr('title', $(this).text());
    });

    //$('.skuList label').click(function() { $(this).toggleClass('sku-picked') });


    var $colorViewObject;
    var $colorViewTimer;
    $('.Cor .skuList label').hover(
        function() {
            $colorViewObject = $(this).parent().parent().parent().parent().find('.viewColor');
            $colorViewObject.find('.title').text($(this).text());
            $colorViewObject.css({
                'background-color': $(this).css('background-color'),
                'background-image': $(this).css('background-image'),
                'background-position': 'center center',
                'left': 0,
                'display': 'none'
            }).stop(true, true).fadeIn();
        },
        function() {
            $colorViewObject.stop(true, true).fadeOut(10);
        }
    );

    $('.buy-in-page-quantity').val(1);
    if ($('.relacionados ul').length > 0) {
        $('.relacionados ul li').hover(
            function() {
                if ($(this).find('.percent').is(':not(:visible)')) {
                    $(this).find('.percent').stop(true, true).fadeIn();
                }
            },
            function() {
                $(this).find('.percent').stop(true, true).fadeOut();
            }
         );
    }
});

$(document).ajaxStop(function() {
    if ($('.kit').length > 0) {
        $('.buy-in-page-button').click(function() {
            var offset = $('#grade').offset();

            offset = offset.top + 'px';

            $('html,body').stop(true, true).animate({ scrollTop: offset }, 1000);
        });
    }
    $('.cart-items-detail thead tr th:eq(0)').text('Itens adicionados ao carrinho').css("width", "400px");
    $('.cart-items-detail thead tr th:eq(1)').text('Quantidade').css("text-align", "center");
});